package services;

import java.util.ArrayList;
import java.util.List;

import utils.Utils;

import dao.Filter;
import dao.ObjectDao;

import business.Button;
import business.User;
import business.UserCompanyCampaign;

public class UserService {
	
	public final static Long USUARIO_EDITOR_ID = 1L;
	public final static Long USUARIO_REVISOR_ID = 2L;
	
	public final static String BLOWFISH_KEY = "INDEC-SULPQ";
	
	public List<User> getUsersByTypeAndCompany(Long type, String cuit)
	{
		ObjectDao dao= new ObjectDao();
		List<Filter> filters = new ArrayList<Filter>();
		Filter filter = new Filter(Filter.STRING_FILTER, "UE.CUIT_EMPRESA", Filter.EQUAL_OPERATOR,cuit,true);
		filters.add(filter);
		filter = new Filter(Filter.NUMERIC_FILTER, "UE.ID_TIPO_USUARIO", Filter.EQUAL_OPERATOR,type,true);
		filters.add(filter);
		return dao.getUsers(filters, ObjectDao.AND_OPERATOR);
	}
	
	public List<User> getUsersByUserAndPass(String user, String pass) throws Exception
	{
		ObjectDao dao= new ObjectDao();
		List<Filter> filters = new ArrayList<Filter>();
		Filter filter = new Filter(Filter.STRING_FILTER, "UPPER(US.USERNAME)", Filter.EQUAL_OPERATOR,user.toUpperCase(),true);
		filters.add(filter);
		filter = new Filter(Filter.STRING_FILTER, "US.PASSWORD", Filter.EQUAL_OPERATOR,Utils.sha256Encoder(pass),true);
		filters.add(filter);
		return dao.getUsers(filters, ObjectDao.AND_OPERATOR);
	}
	
	public UserCompanyCampaign getUserCompanyCampaignByUserIDAndCompanyCampaignID(Long userID, Long companyCampaignID)
	{
		ObjectDao dao= new ObjectDao();
		List<Filter> filters = new ArrayList<Filter>();
		Filter filter = new Filter(Filter.NUMERIC_FILTER, "UEC.ID_USUARIO", Filter.EQUAL_OPERATOR,userID,true);
		filters.add(filter);
		filter = new Filter(Filter.NUMERIC_FILTER, "UEC.ID_EMP_CAM", Filter.EQUAL_OPERATOR,companyCampaignID,true);
		filters.add(filter);
		
		List<UserCompanyCampaign> userCompanyCampainList = dao.getUserCompanyCampains(filters, ObjectDao.AND_OPERATOR);
	
		if(userCompanyCampainList != null && userCompanyCampainList.size() == 1)
		{
			return userCompanyCampainList.get(0);
		}
		else
		{
			return null;
		}	
	}
	
	public List<Button> getButtons()
	{
		ObjectDao dao= new ObjectDao();
		List<Filter> filters = new ArrayList<Filter>();
		
		List<Button> buttonList = dao.getButtons(filters, ObjectDao.AND_OPERATOR);
	
		if(buttonList != null && buttonList.size() > 0)
		{
			return buttonList;
		}
		else
		{
			return null;
		}
	}
	
	public List<Button> getButtonsByRoleAndPollStatus(String role,Long pollStatus)
	{
		ObjectDao dao= new ObjectDao();
		List<Filter> filters = new ArrayList<Filter>();
		Filter filter = new Filter(Filter.STRING_FILTER, "BREE.ID_ROL", Filter.EQUAL_OPERATOR,role,true);
		filters.add(filter);
		filter = new Filter(Filter.NUMERIC_FILTER, "BREE.ID_EST_ENC", Filter.EQUAL_OPERATOR,pollStatus,true);
		filters.add(filter);
		
		List<Button> buttonList = dao.getButtons(filters, ObjectDao.AND_OPERATOR);
	
		if(buttonList != null && buttonList.size() > 0)
		{
			return buttonList;
		}
		else
		{
			return null;
		}
	}
	
	public String getFormButtonsStringByRoleAndPollStatus(String role,Long pollStatus)
	{
		List<Button> buttons = getButtonsByRoleAndPollStatus(role,pollStatus);
		StringBuilder strBuild = new StringBuilder();
		String str = null;
		if(buttons != null && buttons.size() > 0)
		{
			for(Button button : buttons)
			{
				//Valido si no es un boton de encuesta, que sea boton de formulario (Que no contenga el prefijo 'Enc')
				if(!button.getButton().contains("Enc"))
				{
					strBuild.append(button.getButton());
					strBuild.append("|");	
				}				
			}
			if(strBuild != null && strBuild.length() > 0)
			{
				str = strBuild.toString().substring(0,strBuild.length() - 1);
			}	
		}		
		return str;
	}
	
	public List<String> getEmailsToSendNotifications(Long companyCampaignID)
	{
		ObjectDao dao= new ObjectDao();
		return dao.getEmailsToSendNotifications(companyCampaignID);
	}	
}
